//Download by http://www.NewXing.com
// DropView.cpp : implementation file
//

#include "stdafx.h"
#include "LeoBlock2004.h"
#include "DropView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDropView

//IMPLEMENT_DYNCREATE(CDropView, CView)

CDropView::CDropView()
{
	m_pFrameWnd=NULL;
	m_Url="";
	m_Comment="";
	m_TempPoint=0;
	m_bMove=FALSE;
	m_bAddJob=FALSE;
	m_nThreadNum=0;
	m_nAnimate=0;
	m_bIsLButtonUp=FALSE;
	m_bIsTipShow=FALSE;

}

CDropView::~CDropView()
{
}


BEGIN_MESSAGE_MAP(CDropView, CView)
	//{{AFX_MSG_MAP(CDropView)
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_RBUTTONDOWN()
	ON_WM_DESTROY()
	ON_WM_LBUTTONUP()
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDropView drawing
BOOL CDropView::CreateView(LPCTSTR szClassName,CRect rect,CWnd* pParentWnd,UINT nID,DWORD dwStyle)
{
	BOOL bReturn=Create(szClassName,NULL,dwStyle,rect,pParentWnd,nID);

	CRect Rect;
	GetClientRect(&Rect);
	m_ToolTip.Create(this,TTS_ALWAYSTIP);
	m_ToolTip.AddTool(this,"Leo",&Rect,1);
	m_ToolTip.SetMaxTipWidth(4);

	return bReturn;
}
void CDropView::OnDraw(CDC* pDC)
{
	//CDocument* pDoc = GetDocument();
	// TODO: add draw code here
	CRect nRect;
	GetClientRect(nRect);
	//int x=nRect.right/2-12;
	//int y=nRect.bottom/2-8;
	//pDC->TextOut(x,y,"Leo");
}

/////////////////////////////////////////////////////////////////////////////
// CDropView diagnostics

/*#ifdef _DEBUG
void CDropView::AssertValid() const
{
	CView::AssertValid();
}

void CDropView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG*/

/////////////////////////////////////////////////////////////////////////////
// CDropView message handlers

int CDropView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	m_dropTarget.Register(this);

	return 0;
}

DROPEFFECT CDropView::OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point) 
{
	// TODO: Add your specialized code here and/or call the base class
	FORMATETC fmtetc;
	fmtetc.cfFormat=CF_TEXT;
	fmtetc.dwAspect=DVASPECT_CONTENT;
	fmtetc.ptd=NULL;
	fmtetc.tymed=TYMED_HGLOBAL;
	fmtetc.lindex=-1;
	hObj=pDataObject->GetGlobalData(CF_TEXT,&fmtetc);

	return CView::OnDragEnter(pDataObject, dwKeyState, point);
}

DROPEFFECT CDropView::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(hObj!=NULL)
		return DROPEFFECT_MOVE;
	else
		return DROPEFFECT_NONE;

	//return CView::OnDragOver(pDataObject, dwKeyState, point);
}

BOOL CDropView::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point) 
{
	// TODO: Add your specialized code here and/or call the base class
	FORMATETC fmtetc;
	if(hObj!=NULL)
	{
		pDataObject->BeginEnumFormats();
		while(pDataObject->GetNextFormat(&fmtetc))
		{
			CFile *file=new CFile();
			file=pDataObject->GetFileData(CF_TEXT,&fmtetc);
			if(file->GetLength()>0x4c)
			{
				long lOffset=file->Seek(0x4c,CFile::begin);
				char buf[2];
				buf[1]=0x0;
				m_Comment.Empty();
				file->Read(buf,1);
				while(buf[0]!=NULL)
				{
					m_Comment+=buf;
					file->Read(buf,1);
				}
				delete file;
				int nLength=m_Comment.GetLength();
				m_Comment=m_Comment.Left(nLength-4);
				break;
			}
		}
		m_Url=CString((LPSTR)GlobalLock(hObj));
		GlobalUnlock(hObj);
		GlobalFree(hObj);
		m_bAddJob=TRUE;
	}
	
	Invalidate();

	return CView::OnDrop(pDataObject, dropEffect, point);
}

void CDropView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_TempPoint=point;
	//Grab the Mouse
	SetCapture();
	CView::OnLButtonDown(nFlags, point);
}

void CDropView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CPoint pt;
	::GetCursorPos(&pt);
	TOOLINFO toolinfo;
	ScreenToClient(&pt);
	if(m_ToolTip.HitTest(this,pt,&toolinfo) && !(nFlags & MK_LBUTTON))
	{
		
		BTOOLTEXT stToolText;
		
		CString str;
			UINT nRT=::SendMessage(m_pFrameWnd->GetSafeHwnd(),WM_BTOOLEXTENTCX,0,(LPARAM)&stToolText);
			if(nRT)
			{
				str=stToolText.strText;
				m_ToolTip.UpdateTipText((LPCTSTR)str,this,1);
				m_ToolTip.SetMaxTipWidth(600);
			}
		//m_ToolTip.SetMaxTipWidth(639);
	}
	else
		m_bIsTipShow=FALSE;

	m_bIsLButtonUp=FALSE;
	

	if(nFlags & MK_LBUTTON)
	{
		CPoint nPoint;
		::GetCursorPos(&nPoint);
		dropPos.x=nPoint.x-m_TempPoint.x;
		dropPos.y=nPoint.y-m_TempPoint.y;
		GetParent()->SendMessage(WM_DROPWINPOS,0,(LPARAM)&dropPos);
		m_bMove=TRUE;

	}
	
	CView::OnMouseMove(nFlags, point);
}

void CDropView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	PopupDropMenu();
	CView::OnRButtonDown(nFlags, point);
}

BOOL CDropView::PopupDropMenu()
{
	if(m_pFrameWnd && m_nIDMenu)
	{
		CMenu menu;
		if(!menu.LoadMenu(m_nIDMenu))
			return FALSE;
		
		CMenu *pSubMenu=menu.GetSubMenu(0);
		if(!pSubMenu)
			return FALSE;
		
		CPoint pos;
		GetCursorPos(&pos);

		::TrackPopupMenu(pSubMenu->m_hMenu,0,pos.x,pos.y,0,m_pFrameWnd->m_hWnd,NULL);
		//SendMessage to MainFrame
		//::SendMessage(m_pFrameWnd->m_hWnd,WM_DROPView,0,(LPARAM)&nListInfo);
		return TRUE;
	}

	return FALSE;

}
void CDropView::SetWndID(CWnd *pWnd,UINT nID)
{
	m_pFrameWnd=pWnd;
	m_nIDMenu=nID;

}


void CDropView::OnDestroy() 
{
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	delete this;
}



void CDropView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//Release the Mouse
	m_bIsLButtonUp=TRUE;
	ReleaseCapture();
	CView::OnLButtonUp(nFlags, point);


}

void CDropView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	//SendMessage to MainFram Call OnDropwinAddJob()
	
	if(m_bAddJob)
	{
		m_bAddJob=FALSE;
		DROP_URL pDropUrl;
		pDropUrl.strLocatinUrl=m_Url;
		pDropUrl.strComment=m_Comment;
		::SendMessage(m_pFrameWnd->m_hWnd,WM_DROPVIEW,0,(LPARAM)&pDropUrl);
	}
	
	// Do not call CView::OnPaint() for painting messages
}

void CDropView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class



}

BOOL CDropView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	CRect nRect;
	GetClientRect(nRect);

	CDC *mdc=new CDC;
	mdc->CreateCompatibleDC(pDC);
	CBitmap *bitmap=new CBitmap;
	if(m_nThreadNum<=0)
		bitmap->LoadBitmap(IDB_BITMAP5);
	else
	{
		m_nAnimate++;
		switch(m_nAnimate)
		{
		case 1:
			bitmap->LoadBitmap(IDB_BMP_ANIMATE1);
			break;
		case 2:
			bitmap->LoadBitmap(IDB_BMP_ANIMATE2);
			break;
		case 3:
			bitmap->LoadBitmap(IDB_BMP_ANIMATE3);
			break;
		case 4:
			bitmap->LoadBitmap(IDB_BMP_ANIMATE4);
			break;
		case 5:
			bitmap->LoadBitmap(IDB_BMP_ANIMATE5);
			break;
		case 6:
			bitmap->LoadBitmap(IDB_BMP_ANIMATE6);
			break;
		case 7:
			bitmap->LoadBitmap(IDB_BMP_ANIMATE7);
			break;
		case 8:
			bitmap->LoadBitmap(IDB_BMP_ANIMATE8);
			break;
		case 9:
			bitmap->LoadBitmap(IDB_BITMAP5);
			break;

		}
		
	}
	mdc->SelectObject(bitmap);
	pDC->BitBlt(0,0,nRect.right,nRect.bottom,mdc,0,0,SRCCOPY);
	if(mdc)
		delete mdc;
	if(bitmap)
		delete bitmap;
	
	pDC->SetBkMode(TRANSPARENT);

	return CView::OnEraseBkgnd(pDC);
}

void CDropView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_nAnimate>=9)
		m_nAnimate=0;

	Invalidate();

	CView::OnTimer(nIDEvent);
}

void CDropView::SetTimerStart(int iThreadNum)
{
	m_nThreadNum=iThreadNum;
	SetTimer(1,500,NULL);

}

void CDropView::KillTimerEnd(int iThreadNum)
{
	if(iThreadNum<=0)
	{
		m_nThreadNum=iThreadNum;
		KillTimer(1);
		Invalidate();
	}
}

BOOL CDropView::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(m_ToolTip)
		m_ToolTip.RelayEvent(pMsg);
	
	return CView::PreTranslateMessage(pMsg);
}

